﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Xml;
public class CConfigWriter
{
    public CConfigWriter()
    {

    }

    private XmlWriter writer = null;
    System.IO.FileStream sr = null;
    public void CreateXW(String strFileName)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        sr = new System.IO.FileStream(strFileName, System.IO.FileMode.Create);
        writer = XmlWriter.Create(sr, settings);
        writer.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
        //writer.WriteStartDocument();
        writer.WriteStartElement("configuration");
        writer.WriteStartElement("appSettings");
    }
    public void AddXW(string strName, String strValue)
    {
        writer.WriteStartElement("add");

        writer.WriteStartAttribute("key");
        writer.WriteValue(strName);
        writer.WriteEndAttribute();

        writer.WriteStartAttribute("value");
        writer.WriteValue(strValue);
        writer.WriteEndAttribute();

        writer.WriteEndElement(); 
 
    }
    public void CloseXW()
    {
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Close();
        sr.Close();
        sr.Dispose();
    }



    System.Configuration.Configuration cf = null;
    System.Configuration.AppSettingsSection appSection = null;
    public void LoadFile(String strFileName)
    {
        cf = System.Configuration.ConfigurationManager.OpenExeConfiguration(strFileName);
        appSection = (System.Configuration.AppSettingsSection)cf.GetSection("appSettings");
    }

    public void AddElement(String strName)
    {
        appSection.Settings.Add(System.IO.Path.GetFileName(strName).ToLower(), strName);
    }

    public void Save()
    {
        cf.Save();
    }

    public void SaveAs(String strFileName)
    {
        cf.SaveAs(strFileName);
    }

   

}